<template>
  <div>
    <el-table
      :data="tableData"
      style="width: 100%"
    >
      <el-table-column
        type="index"
        label="序号"
        width="100"
      />
      <el-table-column
        prop="skuName"
        label="名称"
        width="180"
      />
      <el-table-column
        prop="skuDesc"
        label="描述"
      />
      <el-table-column
        label="默认图片"
      >
        <template slot-scope="scope">
          <img :src="scope.row.skuDefaultImg" style="width: 100px;height: 100px">
        </template>
      </el-table-column>
      <el-table-column
        prop="weight"
        width="100"
        label="重量"
      />
      <el-table-column
        prop="price"
        width="100"
        label="价格"
      />
      <el-table-column
        prop="category3Id"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button v-if="scope.row.isSale==1" type="success" icon="el-icon-bottom" size="mini" @click="changeIsSale(scope,1)" />
          <el-button v-if="scope.row.isSale==0" type="success" icon="el-icon-top" size="mini" @click="changeIsSale(scope,0)" />
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="$message.info('正在开发中！')" />
          <el-button type="info" icon="el-icon-message" size="mini" @click="isShowDrawer(scope)" />
          <el-button type="warning" icon="el-icon-star-off" size="mini" />
        </template>
      </el-table-column>
    </el-table>
    <!--    分页这块-->
    <el-pagination
      :current-page="pageNo"
      :page-sizes="[3, 5, 10, 15]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!--    抽屉这块-->
    <el-drawer
      :visible.sync="show"
      :direction="direction"
      size="50%"
    >
      <el-row>
        <el-col :span="5">
          <div class="text">名称</div>
        </el-col>
        <el-col :span="16">
          <div>{{ skuInfo.skuName }}</div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">
          <div class="text">描述</div>
        </el-col>
        <el-col :span="16">
          <div>{{ skuInfo.skuDesc }}</div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">
          <div class="text">价格</div>
        </el-col>
        <el-col :span="16">
          <div>{{ skuInfo.price }}</div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">
          <div class="text">平台属性</div>
        </el-col>
        <el-col :span="16">
          <!--          <div>{{ skuInfo.skuAttrValueList }}</div>-->
          <el-tag v-for="(tag,index) in skuInfo.skuAttrValueList" :key="tag.id">{{ tag.attrId }}-{{ tag.valueId }}</el-tag>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">
          <div class="text">商品图片</div>
        </el-col>
        <el-col :span="16">
          <el-carousel trigger="click"  height="200px">
            <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
              <img :src="item.imgUrl" style="width: 100%;height: 100%">
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
import { reqDownSale, reqgetSku, reqSale, reqSkuList } from '@/api/sku'

export default {
  name: 'Sku',
  data() {
    return {
      tableData: [],
      pageNo: 1,
      pageSize: 10,
      total: 1,
      show: false,
      direction: 'rtl',
      skuInfo: {}
    }
  },
  mounted() {
    this.getskuData()
  },
  methods: {
    async  getskuData() {
      const res = await reqSkuList(this.pageNo, this.pageSize)
      if (res.code === 200) {
        this.tableData = res.data.records
        this.total = res.data.total
        console.log(res.data)
      }
    },
    handleSizeChange(pageSize) {
      console.log('pageSize', pageSize)
      this.pageSize = pageSize
      this.getskuData()
    },
    handleCurrentChange(pageNo) {
      console.log('pageNo', pageNo)
      this.pageNo = pageNo
      this.getskuData()
    },
    async changeIsSale(scope, type) {
      if (type === 1) {
        const res = await reqSale(scope.row.id)
        if (res.code === 200) {
          // eslint-disable-next-line require-atomic-updates
          scope.row.isSale = 0
          this.$message.success('上架成功')
        }
      } else {
        const res = await reqDownSale(scope.row.id)
        if (res.code === 200) {
          // eslint-disable-next-line require-atomic-updates
          scope.row.isSale = 1
          this.$message.error('下架成功')
        }
      }
    },
    // 点击打开抽屉
    async  isShowDrawer(scope) {
      this.show = true
      console.log(scope.row.id)
      const res = await reqgetSku(scope.row.id)
      console.log(res)
      if (res.code === 200) {
        this.skuInfo = res.data
      }
    }
  }
}
</script>
<style>
.text{
 font-weight: bold;
  font-size: 18px;
  text-align: right;
  margin-right: 10px;
}
.el-col{
  margin-top: 30px;
}
.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n+1) {
  background-color: #d3dce6;
}
</style>
<style scoped>

</style>
